package com.producer.config;

import com.producer.utils.JacksonUtil;
import feign.RequestTemplate;
import feign.codec.EncodeException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.support.SpringEncoder;

import java.lang.reflect.Type;
import java.util.Objects;

/**
 * @author Liunh
 */
@Slf4j
public class FeignClientEncoder extends SpringEncoder {

    public FeignClientEncoder(ObjectFactory<HttpMessageConverters> messageConverters) {
        super(messageConverters);
    }

    @Override
    public void encode(Object object, Type bodyType, RequestTemplate template) throws EncodeException {
        try {
            super.encode(object, bodyType, template);
            template.body(Objects.requireNonNull(JacksonUtil.beanToJson(object)));
        } catch (Exception ex) {
            log.error(">>>>>> FeignClientEncoder error! ", ex);
        }
    }

}
